package com.zhl.controller;

import com.zhl.entity.Function;
import com.zhl.entity.Role;
import com.zhl.entity.User;
import com.zhl.entity.ZhlResult;
import com.zhl.entity.pojo.Menu;
import com.zhl.service_.RoleService;
import com.zhl.service_.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by Administrator on 2018/10/8.
 */
@Controller
public class pageController {

    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

    @RequestMapping({"/index", "/"})
    public String index() {
        return "index.html";
    }

    /*@GetMapping(value = "/{page}")
    public String page(@PathVariable("page") String page) {
        return page+".html";
    }*/

    @RequestMapping("/about")
    public String about() {
        return "about.html";
    }
    @RequestMapping("/article")
    public String article() {
        return "article.html";
    }
    @RequestMapping("/context")
    public String context() {
        return "context.html";
    }
    @RequestMapping("/link")
    public String link() {
        return "link.html";
    }
    @RequestMapping("/login")
    public String login( Model model) {
        model.addAttribute("msg","");
        return "login.html";
    }
    @RequestMapping("/message")
    public String message() {
        return "message.html";
    }
    @RequestMapping("/unauthorized")
    public String unauthorized() {
        return "unauthorized.html";
    }
    @RequestMapping("/zuijimo")
    public String zuijimo() {
        return "zuijimo.html";
    }


    @RequestMapping("/admin/login")
    public String loginPage() {
        return "admin/login.html";
    }

    @RequestMapping("/admin/index")
    public String aindex() {
        return "admin/index.html";
    }
    @RequestMapping("/admin/about")
    public String aabout() {
        return "admin/about.html";
    }
    @RequestMapping("/admin/article")
    public String aarticle() {
        return "admin/article.html";
    }
    @RequestMapping("/admin/context")
    public String acontext() {
        return "admin/context.html";
    }
    @RequestMapping("/admin/link")
    public String alink() {
        return "admin/link.html";
    }
    @RequestMapping("/admin/message")
    public String amessage() {
        return "admin/message.html";
    }
    @RequestMapping("/admin/function")
    public String function() {
        return "admin/function.html";
    }
    @RequestMapping("/admin/user")
    public String user() {
        return "admin/user.html";
    }
    @RequestMapping("/admin/role")
    public String role() {
        return "admin/role.html";
    }
    @RequestMapping("/admin/music")
    public String music() {
        return "admin/music.html";
    }
    @RequestMapping("/admin/publish")
    public String publish() {
        return "admin/publish.html";
    }
    @RequestMapping("/admin/modify")
    public String modify() {
        return "admin/modify.html";
    }
    @RequestMapping("/admin/roledetail")
    public String roledetail() {
        return "admin/roledetail.html";
    }
    @RequestMapping("/admin/apply")
    public String apply() {
        return "admin/apply.html";
    }
    @RequestMapping("/admin/addfunction")
    public String addfunction() {
        return "admin/addfunction.html";
    }


    /**
     * 根据权限显示目录
     */
    @RequestMapping("/menu/getmenu")
    @ResponseBody
    public ZhlResult getMenu(HttpServletRequest request) {

        User user = (User)request.getSession().getAttribute("user");

        User userByid = userService.queryUserByid(user.getId());
        List<Role> roles = userByid.getRoles();

        ArrayList<Menu> menus = new ArrayList<>();
        for (Role role:roles) {
            Role roleByid = roleService.queryRoleByid(role.getRid());
            List<Function> functions = roleByid.getFunctions();
            for (Function function: functions) {
                Menu menu = new Menu();
                menu.setName(function.getName());
                menu.setUrl(function.getUrl());
                menus.add(menu);

            }

        }

        //去重
        Set<Menu> menuLinkedHashSet = new LinkedHashSet<Menu>(menus);
        menus = new ArrayList<Menu>(menuLinkedHashSet);
        return ZhlResult.ok(menus);

    }



}
